/* Introducing the OpenGL ES 2 Vertex shader
 *
 * The main loop inside the vertex shader gets executed
 * one time for each vertex.
 *
 *      vertex -> *       uniform data -> mat4 projection = ( 1, 0, 0, 0,
 *      (0,1,0)  / \                                          0, 1, 0, 0,
 *              / . \  <- origo (0,0,0)                       0, 0, 1, 0,
 *             /     \                                        0, 0,-1, 1 );
 *  vertex -> *-------* <- vertex
 *  (-1,-1,0)             (1,-1,0) <- attribute data can be used
 *                        (0, 0,1)    for color, position, normals etc.
 *
 * The vertex shader recive input data in form of
 * "uniform" data that are common to all vertex
 * and
 * "attribute" data that are individual to each vertex.
 * One vertex can have several "attribute" data sources enabled.
 *
 * The vertex shader produce output used by the fragment shader.
 * gl_Position are expected to get set to the final vertex position.
 * You can also send additional user defined
 * "varying" data to the fragment shader.
 *
 * Model Translate, Scale and Rotate are done here by matrix-multiplying a
 * projection matrix against each vertex position.
 *
 * The whole vertex shader program are a String containing GLSL ES language
 * http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf
 * sent to the GPU driver for compilation.
 */
 
 


// For GLSL 1 and 1.1 code i highly recomend to not include a 
// GLSL ES language #version line, GLSL ES section 3.4
// Many GPU drivers refuse to compile the shader if #version is different from
// the drivers internal GLSL version.
//
// This demo use GLSL version 1.1 (the implicit version)


#if __VERSION__ >= 130 // GLSL 130+ uses in and out
  #define attribute in // instead of attribute and varying 
  #define varying out  // used by OpenGL 3 core and later. 
#endif 

#ifdef GL_ES 
precision mediump float;  // Precision Qualifiers
precision mediump int;    // GLSL ES section 4.5.2
#endif 


//in -- unifrom
uniform mat4    modelview ;  // Incomming data used by the vertex shader (uniform and attributes)
uniform mat4    projection;  
//uniform mat3	normalMatrix; // no need since light position is model view system based
uniform vec4	lightPosition;
uniform vec2	ambiantDiffuse;
uniform vec4	color;
uniform vec3	normal;

//in -- attributes
attribute vec3  attribute_Position;  
attribute vec3  attribute_Normal;  
attribute vec4  attribute_Color;  
attribute vec2	attribute_Texture;   


//out
varying vec4    varying_Color;  
varying vec2	coordTexture;


void main(void) 
{ 
  
  // position
  gl_Position = projection * modelview * vec4(attribute_Position, 1.0); 
  
  // color with light
  vec3 n;
  if (normal.x == 2){ // then use per-vertex normal
  	n = attribute_Normal;
  }else{
  	n = normal;
  }
  float factor = dot(n, lightPosition);
  if (factor < 0){
  	factor = 0;
  }
  float ambiant = ambiantDiffuse[0];
  float diffuse = ambiantDiffuse[1];
  vec4 c;
  if (color[0] < 0){ // then use per-vertex-color
  	c = attribute_Color;
  }else{ // use per-object-color
  	c = color;
  }
  
  
  varying_Color.xyz = c.xyz * (ambiant + diffuse * factor);
  varying_Color.a = c.a;
  
  
      
  // texture
  coordTexture = attribute_Texture;
                        
  
} 